package com.kiwi.cases.mario;

import com.kiwi.action.LoginAction;
import com.kiwi.constant.Constant;
import com.kiwi.util.APIEncryptSet;

import com.netease.shockwave.http.HttpHandler;
import com.netease.shockwave.http.HttpUtil;
import com.netease.shockwave.testng.ShockwaveTestNG;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;


/**
 * 测试接口：密码登录接口 http://uc.s.kiwi.com/api/login
 *

 * @author bjwangzheng3
 */
public class marioTest extends ShockwaveTestNG
{
    private Logger logger = Logger.getLogger(marioTest.class);
    @Autowired
    private testStompClientEndpoint StompClient;
    @BeforeClass
    public void BeforeClassTest()
    {
        logger.info("客户端：匹配开始");

    }
    public HashMap<String,String> encodeParam(String gameId,String accountId,String sessionId,String apiLevel) throws UnsupportedEncodingException {
        String param = String.format("gameId=%s&accountId=%s&sessionId=%s&apiLevel=%s", gameId,
                accountId,sessionId,apiLevel);
        String encodeParam=APIEncryptSet.enCode(param);
        String data=encodeParam.split("&")[0];
        String stamp=encodeParam.split("&")[1];
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("data", data.substring(data.indexOf("=")+1));
        params.put("stamp", stamp.substring(stamp.indexOf("=")+1));
        return params;
    }

    @Test(description = "客户端匹配接口测试", dataProvider = "MatchData")
    public void enterMatchtest(String gameId,String accountId,String sessionId,String apiLevel) throws Exception {
        long id = Thread.currentThread().getId();
        System.out.println("MatchResultTest Thread. Thread id is: " + id);
        HttpHandler handler = HttpHandler.getInstance();
        String url = "http://api.game.hiwan360.com/match/enter";
        HashMap<String, String> params = encodeParam(gameId, accountId, sessionId, apiLevel);
        String enResult = handler.usingPostMethod(url, params, null);
        logger.info("enterMatch接口返回结果"+enResult);

    }
    @Test(description = "客户端匹配接口测试", dataProvider = "MatchResultData")
    public void matchResultTest(String gameId,String accountId,String sessionId,String apiLevel) throws Exception {
        long id = Thread.currentThread().getId();
        System.out.println("MatchResultTest Thread. Thread id is: " + id);
        HttpHandler handler = HttpHandler.getInstance();
        String roomId = null;
        String resulturl = "http://api.game.hiwan360.com/match/result";
        HashMap<String, String> params = encodeParam(gameId, accountId, sessionId, apiLevel);
        for (int i = 0; i < 10; i++) {
            String Result = handler.usingPostMethod(resulturl, params, null);
            logger.info("匹配结果："+Result);
            JSONObject jsonRes = HttpUtil.parseJSONObject(Result);
            logger.info(jsonRes.get("result"));
            if (jsonRes.get("result").toString().equals("100")&&(!jsonRes.getJSONObject("data").isEmpty())) {
                roomId = jsonRes.getJSONObject("data").get("roomId").toString();
                String myUid=accountId;
                String opponentUid=((JSONObject)jsonRes.getJSONObject("data").getJSONArray("userList").get(0)).getString("accountId");
                logger.info("匹配结果房间号："+roomId+" 我的账号："+myUid+" 对手的账号："+opponentUid);
//                StompClient.StompClientEndpoint(roomId,myUid,opponentUid);
                break;
            }
            Thread.sleep(5000);
        }
    }

    @DataProvider(name = "MatchData", parallel=true)
    public static Object[][] data1(){
        return new Object[][]{
                { "gameTest1","888888888","helloworld","1"},
                { "gameTest1","888888889","helloworld","1"},
                { "gameTest1","888888890","helloworld","1"},
                { "gameTest1","888888891","helloworld","1"},
                { "gameTest1","888888892","helloworld","1"},
                { "gameTest1","888888893","helloworld","1"},
                { "gameTest1","888888894","helloworld","1"},
                { "gameTest1","888888895","helloworld","1"},
                { "gameTest1","888888896","helloworld","1"},
                { "gameTest1","888888897","helloworld","1"},
                { "gameTest1","888888898","helloworld","1"},
                { "gameTest1","888888899","helloworld","1"},
                { "gameTest1","888888900","helloworld","1"},
                { "gameTest1","888888901","helloworld","1"},};
    }
    @DataProvider(name = "MatchResultData", parallel=true)
    public static Object[][] data(){
        return new Object[][]{
                { "gameTest1","888888888","helloworld","1"},
                { "gameTest1","888888889","helloworld","1"},
                { "gameTest1","888888890","helloworld","1"},
                { "gameTest1","888888891","helloworld","1"},
                { "gameTest1","888888892","helloworld","1"},
                { "gameTest1","888888893","helloworld","1"},
                { "gameTest1","888888894","helloworld","1"},
                { "gameTest1","888888895","helloworld","1"},
                { "gameTest1","888888896","helloworld","1"},
                { "gameTest1","888888897","helloworld","1"},
                { "gameTest1","888888898","helloworld","1"},
                { "gameTest1","888888899","helloworld","1"},
                { "gameTest1","888888900","helloworld","1"},
                { "gameTest1","888888901","helloworld","1"},};
    }
    @AfterClass
    public void AfterClassTest()
    {

        logger.info("客户端：匹配接口测试结束");
    }


}
